package com.beemans.common.utils

/**
 * @author tiamosu
 * @date 2021/3/13
 *
 * 描述：时间倒计时帮助类，默认倒计时60S
 */
class CountDownTimer {
    private var descTime = 60 * 1000L            //倒计时长，默认为60秒
    var isCountDowning = false                   //是否正在执行倒计时
        get() = countDownService.isCountDowning
        private set

    private var millis = 0L
    private var countDownCallback: CountDownCallback? = null

    /**
     * 倒计时服务
     */
    private val countDownService by lazy { CountDownService() }

    init {
        setCountTime(60)
    }

    /**
     * @param time 倒计时时间，单位为秒
     */
    fun setCountTime(time: Long) {
        descTime = time * 1000
        clearTimer()
    }

    /**
     * 开始倒计时
     */
    fun start(callback: CountDownCallback.() -> Unit = {}) {
        countDownCallback = CountDownCallback().apply(callback)
        if (isCountDowning) {
            return
        }
        millis = descTime

        countDownCallback?.onStart?.invoke()
        countDownService.startTask({
            setData()
        })
    }

    private fun setData() {
        val sec = (millis / 1000).toInt()
        countDownCallback?.onTick?.invoke(sec)

        millis -= 1000
        if (millis < 0) {
            clearTimer()
            countDownCallback?.onFinish?.invoke()
        }
    }

    /**
     * 倒计时数据格式化
     *
     * @param sec 倒计时秒数
     * @param isHms 是否为HH:mm:ss格式
     * @param isPaddingZero 是否补零
     */
    fun format(sec: Int, isHms: Boolean = false, isPaddingZero: Boolean = true): String {
        if (!isHms) {
            return if (isPaddingZero && sec < 10 && sec > 0) "0$sec" else "$sec"
        }
        // 格式化小时的格式
        val h = millis / (60 * 60 * 1000)
        // 格式化分钟的格式
        val m = (millis - h * 60 * 60 * 1000) / (60 * 1000)
        // 格式化秒钟的格式
        val s = millis / 1000 % 60

        val hour = if (isPaddingZero && h < 10) "0$h" else "$h"
        val minute = if (isPaddingZero && m < 10) "0$m" else "$m"
        val second = if (isPaddingZero && s < 10) "0$s" else "$s"
        return "$hour:$minute:$second"
    }

    private val hms: Array<String>
        get() {
            // 格式化小时的格式
            val h = millis / (60 * 60 * 1000)
            // 格式化分钟的格式
            val m = (millis - h * 60 * 60 * 1000) / (60 * 1000)
            // 格式化秒钟的格式
            val s = millis / 1000 % 60

            val hour = if (h < 10) "0$h" else "$h"
            val minute = if (m < 10) "0$m" else "$m"
            val second = if (s < 10) "0$s" else "$s"
            return arrayOf(hour, minute, second)
        }

    /**
     * 清除倒计时
     */
    fun clearTimer() {
        isCountDowning = false
        countDownService.stopTask()
    }

    class CountDownCallback {
        internal var onStart: (() -> Unit)? = null
        internal var onTick: ((sec: Int) -> Unit)? = null
        internal var onFinish: (() -> Unit)? = null

        fun onStart(onStart: () -> Unit) {
            this.onStart = onStart
        }

        fun onTick(onTick: (sec: Int) -> Unit) {
            this.onTick = onTick
        }

        fun onFinish(onFinish: () -> Unit) {
            this.onFinish = onFinish
        }
    }
}